import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @author penghao
 */
@Slf4j
public class Main {
    public static String getMacAddress() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                if (!networkInterface.isUp() || networkInterface.isLoopback() || networkInterface.isVirtual())
                    continue; // 忽略非活动、回环和虚拟接口
                byte[] mac = networkInterface.getHardwareAddress();
                if (mac != null) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
                    }
                    return sb.toString(); // 返回MAC地址
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "MAC address not found";
    }

    public static String getCpuNum() {
        try {
            //  Process process = Runtime.getRuntime().exec("wmic cpu get ProcessorID");
            String[] linux = {"/bin/bash", "-c", "dmidecode -t processor | grep 'ID' | awk -F ':' '{print $2}' | head -n 1"};
            String[] windows = {"wmic", "cpu", "get", "ProcessorId"};

            // 获取系统信息
            String property = System.getProperty("os.name");
            Process process = Runtime.getRuntime().exec(property.contains("Window") ? windows : linux);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (!line.trim().isEmpty() && !line.trim().contains("ProcessorId")) {
                    //System.out.println("CPU序列号: " + line.trim());
                    reader.close();
                    return line.replaceAll(" ", "");
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "CPU SerialNumber not found";
    }

    public static void main(String[] args) throws Exception {
        System.out.println("本机的mac网卡的地址" + getMacAddress());//50:5B:C2:B3:41:FD
        System.out.println("本机的cpu序列号" + getCpuNum());//BFEBFBFF000806EA
    }
}